﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compiled_Tests;
using LEET.CommonInterfaces.TestControls;
using Compiled_Tests.RuleBase;
using System.Windows.Automation;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using Tedds_Tool;
using System.IO;
using System.Windows;

namespace LEET.PlayBack.Controllers
{
    public class ImageCapture
    {
        IBox box;
        Bitmap bmp;
        List<String> tests;
        int count;
        String saveDirectory;

        public ImageCapture(IBox mw)
        {
            this.box = mw;
            count = 0;
            tests = new List<string>();

            if (!Directory.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LEET/LEET_Screen_Captures")))
                Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LEET/LEET_Screen_Captures"));
            saveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("LEET/LEET_Screen_Captures", DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")));
        }

        Rect BoundingRectange
        {
            get
            {
                try
                {
                    return box.RootElement.Current.BoundingRectangle;
                }
                catch
                {
                    return default(Rect);
                }
            }
        }

        private void CaptureScreen()
        {
            int screenWidth;
            int screenHeight;
            int top;
            int left;
            try
            {
                if (BoundingRectange != default(Rect))
                {
                    screenWidth = (int)(BoundingRectange.Width + 0.5);
                    screenHeight = (int)(BoundingRectange.Height + 0.5);
                    top = (int)(BoundingRectange.Top + 0.5);
                    left = (int)(BoundingRectange.Left + 0.5);
                    bmp = new Bitmap(screenWidth, screenHeight);
                    Graphics gfx = Graphics.FromImage((Image)bmp);
                    gfx.CopyFromScreen(left, top, 0, 0,
                        new System.Drawing.Size(screenWidth, screenHeight));
                }
            }
            catch
            {
                try
                {
                    screenWidth = Screen.PrimaryScreen.Bounds.Width;
                    screenHeight = Screen.PrimaryScreen.Bounds.Height;
                    top = Screen.PrimaryScreen.Bounds.Top;
                    left = Screen.PrimaryScreen.Bounds.Left;
                    bmp = new Bitmap(screenWidth, screenHeight);
                    Graphics gfx = Graphics.FromImage((Image)bmp);
                    gfx.CopyFromScreen(left, top, 0, 0,
                        new System.Drawing.Size(screenWidth, screenHeight));
                }
                catch
                {
                    //We really ought to log this.
                }
            }
        }

        private void SaveScreenCapture()
        {
            if (bmp != null)
            {
                String rootTest = tests.First();
                StringBuilder fileName = new StringBuilder();
                fileName.Append(count.ToString("D3"));
                foreach (String test in tests)
                    fileName.Append(".").Append(test);
                fileName.Append(".jpg");

                FileInfo info = new FileInfo(Path.Combine(saveDirectory, rootTest, fileName.ToString()));
                info.Directory.Create();
                bmp.Save(info.FullName, ImageFormat.Jpeg);

                count++;
            }
        }

        internal void OnTestStarted(ITestScript t)
        {
            tests.Add(t.GetType().Name);
            CaptureScreen();
        }

        internal void OnTestFinished()
        {
            CaptureScreen();
            tests.RemoveAt(tests.Count - 1);
        }

        internal void OnActionTaken()
        {
            CaptureScreen();
            SaveScreenCapture();
        }

        internal void OnSignalRecieved(ScriptSignal sig)
        {
            CaptureScreen();
        }

        internal void OnErrorRecieved(Exception e)
        {
            SaveScreenCapture();
            CaptureScreen();
            SaveScreenCapture();
        }

        internal void OnRuleBaseRecieved(IRuleBase rb)
        {
            CaptureScreen();
        }

        internal void OnProcessRecieved(IProcess proc)
        {
            CaptureScreen();
        }

        internal void OnTermination()
        {
            CaptureScreen();
        }
    }
}
